package cn.unknowpupil.controller.monitor;

import cn.unknowpupil.common.core.annotation.Log;
import cn.unknowpupil.common.core.enums.BusinessType;
import cn.unknowpupil.common.core.resopnse.ResultResponse;
import cn.unknowpupil.domain.system.SysLoginInfo;
import cn.unknowpupil.service.system.ISysLoginInfoService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * Created at 2020-10-20 by  w10g <br>
 * Initialize SysLoginInfo  ... <br>
 *  系统访问记录 视图浏览层处理
 *
 * @author unknowpupil
 * @since 0.0.1
 */
@Api(tags = {"系统-系统访问记录模块" }, value = "SysLoginInfo")
@RestController
@RequestMapping(value = "admin/sys-login-info")
@RequiredArgsConstructor
public class SysLoginInfoController {

    private final ISysLoginInfoService sysLoginInfoService;




    /**
     * 删除
     *
     * @param ids {@code Long}
     * @return {@link ResultResponse}
     */
    @Log(title = "remove", businessType = BusinessType.DELETE)
    @DeleteMapping("remove/{ids}")
    public ResultResponse remove(@PathVariable("ids") Long[] ids) {
        // 业务逻辑
        boolean deleted  = sysLoginInfoService.delete(ids);
        if(deleted){
            return ResultResponse.success("删除成功");
        }else{
            return ResultResponse.buildFailure("删除失败");
        }
    }

    @Log(title = "清空登录日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("/clean")
    public ResultResponse clean(){
        boolean remove = sysLoginInfoService.remove(null);
        if(remove){
            return ResultResponse.success("清除成功");
        }else {
            return ResultResponse.buildFailure("清除失败");
        }
    }

    /**
        * 分页
        *
        * @param current {@code int} 页码
        * @param size    {@code int} 笔数
        * @return {@link ResultResponse}
        */
    @GetMapping("page/{current}/{size}")
    public ResultResponse<IPage<SysLoginInfo>> page(@PathVariable("current") int current, @PathVariable("size") int size, @ModelAttribute SysLoginInfo SysLoginInfo) {
            IPage<SysLoginInfo> page = sysLoginInfoService.page(current, size, SysLoginInfo);
            return ResultResponse.buildSuccess(page);
    }
}
